package org.interfaceadaptation.parameter.expression.impl;

import java.util.Arrays;
import java.util.List;

import org.interfaceadaptation.InterfaceAdaptationError;
import org.interfaceadaptation.parameter.expression.Context;
import org.interfaceadaptation.parameter.expression.Expression;

public abstract class AbstractExpression implements Expression {

    Expression parent;
 
    public AbstractExpression(Expression parent) {
        this.parent = parent;
    }

    public AbstractExpressionContext getExpressionContext(Context context) {
        if (context instanceof ContextImpl) {
            AbstractExpressionContext expressionContext = ((ContextImpl) context).getExpressionContext(this);
            if (expressionContext == null) {
                expressionContext = createExpressionContext(context);
            }
            return expressionContext;
        } else {
            throw new InterfaceAdaptationError("Context must be a ContextImpl!");
        }

    }

    protected abstract AbstractExpressionContext createExpressionContext(Context context);

    public Expression getParent() {
        return parent;
    }
    
    public List<String> getAllNodeNames() {
        return Arrays.asList(getDefaultNodeName());
    }


    @Override
    public String toString() {
        return getPath();
    }

    @Override
    public String getPath() {
        if (getParent() == null) {
            return getDefaultNodeName();
        } else {
            return parent.getPath() + "." + getDefaultNodeName();
        }
    }

}
